//
//  HXGalibrationViewController.m
//  HongxuanBluetooth
//
//  Created by 广州弘轩科技发展有限公司 on 2023/9/12.
//

#import "HXGalibrationViewController.h"

@interface HXGalibrationViewController ()
@property (strong, nonatomic) BlueToothManager *blueToothManager;//蓝牙

@property (assign, nonatomic) int hourhands1;
@property (assign, nonatomic) int hourhands2;

@property (assign, nonatomic) int minutehands1;
@property (assign, nonatomic) int minutehands2;
@property (assign, nonatomic) BOOL confirm;
@end

@implementation HXGalibrationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.hourhands1 = 0;
    self.hourhands2 = 0;
    self.minutehands1 = 0;
    self.minutehands2 = 0;
    self.fd_prefersNavigationBarHidden = YES;
    self.blueToothManager = [BlueToothManager shareInstance];
    [self BottomFunction];
    [self enterCalibrationMode];
}
//进入校准模式
-(void)enterCalibrationMode{
    
    Byte send[] = {0x58,0x57,0x03,0x0A,0x00,0x00,0x00,0x01};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
    [self.blueToothManager writeData:sendData];
}
-(void)BottomFunction{
    
    UIImageView *tabr = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavHeight-44, winWidth, 44)];
    [self.view addSubview:tabr];
    UIImageView *mainscreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 44)];
    if ([themeColors isEqualToString:@"default"]) {
        mainscreen.image = [UIImage imageNamed:@"galibrationmian"];//detailmid
    }else{
        mainscreen.image = [UIImage imageNamed:@"galibrationmian"];//detailmid
    }
    
    [tabr addSubview:mainscreen];
    
    NSInteger mastop = 0;
    NSInteger mashands = 0;
    NSInteger masMinute = 0;
    NSInteger mastitle = 0;
    NSInteger masashands = 0;
    if(winHeight<=736){
        mastop = 10;
        mastitle = 5;
        mashands = 10;
        masMinute = 10;
        masashands = 10;
    }else{
        mastop = 27;
        mastitle = 18;
        mashands = 33.33;
        masMinute = 42;
        masashands = 22;
    }
    
//    surface
    UIImageView *surface = [[UIImageView alloc] init];
    surface.image = [UIImage imageNamed:@"biao"];
    [self.view addSubview:surface];
    [surface mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tabr.mas_bottom).offset(mastop);
        make.width.mas_equalTo(145);
        make.height.mas_equalTo(171.33);
        make.centerX.mas_equalTo(self.view);
    }];
    
    UILabel *title = [[UILabel alloc] init];
    title.text = @"Calibrate the pointer offset of your watch Adjust the hour and minute hands by + -  to ensure that they all point in the  12 o'clock direction";
    title.textAlignment = NSTextAlignmentCenter;
    title.numberOfLines = 4;
    title.textColor = [UIColor whiteColor];
    title.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(surface.mas_bottom).offset(18);
        make.height.mas_equalTo(88);
        make.width.mas_equalTo(310);
        make.centerX.mas_equalTo(self.view);
    }];
    
    
//    Hour hands
    UILabel *hourhandslb = [[UILabel alloc] init];
    hourhandslb.text = @"Hour hands";
    hourhandslb.textColor = [UIColor whiteColor];
    hourhandslb.font = [UIFont systemFontOfSize:25];
    hourhandslb.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:hourhandslb];
    [hourhandslb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(title.mas_bottom).offset(mashands);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(130);
        make.centerX.mas_equalTo(self.view);
    }];
    
//    Hourhands-
    UIImageView *hourni = [[UIImageView alloc] init];
    hourni.image = [UIImage imageNamed:@"hands-"];
    hourni.userInteractionEnabled = YES;
    [self.view addSubview:hourni];
    [hourni mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(hourhandslb.mas_bottom).offset(masashands);
        make.left.mas_equalTo(winWidth/4-33);
        make.height.width.mas_equalTo(67);
    }];
    UIButton *hournibt = [UIButton buttonWithType:UIButtonTypeCustom];
    hournibt.tag = 1;
    [hournibt addTarget:self action:@selector(hournibt:) forControlEvents:UIControlEventTouchUpInside];
    [hourni addSubview:hournibt];
    [hournibt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(hourni);
    }];
    
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesture:)];
    //设置长按时间
    longPressGesture.minimumPressDuration = 0.5;
//    把tapGesture（手势）添加到对应的view
    [hournibt addGestureRecognizer:longPressGesture];
    
    
    
    UIImageView *hourshun = [[UIImageView alloc] init];
    hourshun.image = [UIImage imageNamed:@"hands+"];
    hourshun.userInteractionEnabled = YES;
    [self.view addSubview:hourshun];
    [hourshun mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(hourhandslb.mas_bottom).offset(masashands);
        make.right.mas_equalTo(-winWidth/4+33);
        make.height.width.mas_equalTo(67);
    }];
    UIButton *hourshunbt = [UIButton buttonWithType:UIButtonTypeCustom];
    hourshunbt.tag = 1;
    [hourshunbt addTarget:self action:@selector(hourshunbt:) forControlEvents:UIControlEventTouchUpInside];
    [hourshun addSubview:hourshunbt];
    [hourshunbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(hourshun);
    }];
    
    UILongPressGestureRecognizer *longPressGesturejia = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesturejia:)];
    //设置长按时间
    longPressGesturejia.minimumPressDuration = 0.5;
//    把tapGesture（手势）添加到对应的view
    [hourshunbt addGestureRecognizer:longPressGesturejia];
    
    
    UILabel *minutehandslb = [[UILabel alloc] init];
    minutehandslb.text = @"Minute hands";
    minutehandslb.textColor = [UIColor whiteColor];
    minutehandslb.font = [UIFont systemFontOfSize:25];
    minutehandslb.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:minutehandslb];
    [minutehandslb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(hourshun.mas_bottom).offset(masMinute);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(230);
        make.centerX.mas_equalTo(self.view);
    }];
    
    
    
    
    UIImageView *ninuteni = [[UIImageView alloc] init];
    ninuteni.image = [UIImage imageNamed:@"hands-"];
    ninuteni.userInteractionEnabled = YES;
    [self.view addSubview:ninuteni];
    [ninuteni mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(minutehandslb.mas_bottom).offset(masashands);
        make.left.mas_equalTo(winWidth/4-33);
        make.height.width.mas_equalTo(67);
    }];
    UIButton *ninutenibt = [UIButton buttonWithType:UIButtonTypeCustom];
    ninutenibt.tag = 2;
    ninutenibt.userInteractionEnabled = YES;
    [ninutenibt addTarget:self action:@selector(hournibt:) forControlEvents:UIControlEventTouchUpInside];
    [ninuteni addSubview:ninutenibt];
    [ninutenibt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(ninuteni);
    }];
    
    UILongPressGestureRecognizer *longPressGesturetow = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesturetow:)];
    //设置长按时间
    longPressGesturetow.minimumPressDuration = 0.5;
//    把tapGesture（手势）添加到对应的view
    [ninutenibt addGestureRecognizer:longPressGesturetow];
    
    
    UIImageView *ninuteshun = [[UIImageView alloc] init];
    ninuteshun.image = [UIImage imageNamed:@"hands+"];
    ninuteshun.userInteractionEnabled = YES;
    [self.view addSubview:ninuteshun];
    [ninuteshun mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(minutehandslb.mas_bottom).offset(masashands);
        make.right.mas_equalTo(-winWidth/4+33);
        make.height.width.mas_equalTo(67);
    }];
    UIButton *ninuteshunbt = [UIButton buttonWithType:UIButtonTypeCustom];
    ninuteshunbt.tag = 2;
    [ninuteshunbt addTarget:self action:@selector(hourshunbt:) forControlEvents:UIControlEventTouchUpInside];
    [ninuteshun addSubview:ninuteshunbt];
    [ninuteshunbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(ninuteshun);
    }];
    
    UILongPressGestureRecognizer *longPressGesturetowjia = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGesturetowjia:)];
    //设置长按时间
    longPressGesturetowjia.minimumPressDuration = 0.5;
//    把tapGesture（手势）添加到对应的view
    [ninuteshunbt addGestureRecognizer:longPressGesturetowjia];
    
    
    
    
    UIImageView *statebj = [[UIImageView alloc] init];
    if([themeColors isEqualToString:@"default"]){
        statebj.backgroundColor = [UIColor blackColor];
    }else{
        statebj.backgroundColor = midnightRGB;
    }
    [self.view addSubview:statebj];
    [statebj mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(0);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-34);
        make.height.mas_equalTo(50);
    }];
    
#pragma mark 返回入口
    UIImageView *backview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back"]];
    backview.userInteractionEnabled = YES;
    [self.view addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(33);
        make.height.mas_equalTo(36);
        make.left.mas_equalTo(26);
    }];
    
    UIButton *backbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backbt addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:backbt];
    [backbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(backview);
    }];
    
    UIImageView *bluetherview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bluether"]];
    bluetherview.userInteractionEnabled = YES;
    [self.view addSubview:bluetherview];
    [bluetherview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-52);
        make.height.mas_equalTo(13);
    }];
    
    UIButton * confirmbt = [UIButton buttonWithType:UIButtonTypeCustom];
    //给按钮加一个白色的板框
    confirmbt.layer.borderColor = [[UIColor whiteColor] CGColor];
    confirmbt.layer.borderWidth = 1.0f;
            //给按钮设置弧度,这里将按钮变成了圆形
    confirmbt.layer.cornerRadius = 10.0f;
    confirmbt.backgroundColor = [UIColor blackColor];
    confirmbt.layer.masksToBounds = YES;
    [confirmbt setTitle:@"Confirm" forState:UIControlStateNormal];
    [confirmbt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmbt addTarget:self action:@selector(confirmto1010) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmbt];
    [confirmbt mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(ninuteshun.mas_bottom).offset(25);
//        make.right.mas_equalTo(-15);
//        make.width.mas_equalTo(67);
//        make.height.mas_equalTo(45);
        
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(75);
        make.height.mas_equalTo(45);
        make.right.mas_equalTo(-26);
    }];
    
//    UIImageView *bjview = [[UIImageView alloc] init];
//    if ([themeColors isEqualToString:@"default"]) {
//        bjview.image = [UIImage imageNamed:@"detailbj"];
//    }else{
//        bjview.backgroundColor = midnightRGB;
//        bjview.layer.cornerRadius = 8;
//        bjview.layer.masksToBounds = YES;
//    }
    
    
    
    
    

}
- (void)confirmto1010{
    
    self.confirm = YES;
    
    Byte send[] = {0x58,0x57,0x03,0xff,0x00,0x0a,0x0a,0x01};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

    [self.blueToothManager writeData:sendData];
    NSString *time = @"10:10";
    //    存入蓝牙时间
    [[NSUserDefaults standardUserDefaults] setObject:time forKey:@"Bluetoothtime"];
    //这里建议同步存储到磁盘中，但是不是必须的
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"bluetoothTime" object:nil userInfo:@{@"time":time}];
    [self respecifyTheRootView];
}
#pragma mark - 返回根视图
-(void)respecifyTheRootView{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - 小时逆时针调
- (void)hournibt:(UIButton *)button{
    Byte send[] = {0x58,0x57,0x03,0x0A,0x00,0x00,0x00,0x01};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
    [self.blueToothManager writeData:sendData];
    NSLog(@"逆时针调");
    if(button.tag == 1){
        NSLog(@"小时逆时针调");
        self.hourhands1 ++;
        self.hourhands1 = [XHbluetoolUtils intTransitionhex16:self.hourhands1];
        Byte send[] = {0x58,0x57,0x03,0x05,self.hourhands1,0x00,0x00,self.hourhands1};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

        [self.blueToothManager writeData:sendData];
    }
    if(button.tag == 2){
        NSLog(@"分针逆时针调");
        self.minutehands1 ++;
        self.minutehands1 = [XHbluetoolUtils intTransitionhex16:self.minutehands1];
        Byte send[] = {0x58,0x57,0x03,0x07,self.minutehands1,0x00,0x00,self.minutehands1};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

        [self.blueToothManager writeData:sendData];
    }
    
    
}
#pragma mark - 小时顺时针调
- (void)hourshunbt:(UIButton *)button{
    
    Byte send[] = {0x58,0x57,0x03,0x0A,0x00,0x00,0x00,0x01};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
    [self.blueToothManager writeData:sendData];
    NSLog(@"顺时针调");
 
    if(button.tag == 1){
        NSLog(@"小时时针调");
        self.hourhands2 ++;
        self.hourhands2 = [XHbluetoolUtils intTransitionhex16:self.hourhands2];
        Byte send[] = {0x58,0x57,0x03,0x06,self.hourhands2,0x00,0x00,self.hourhands2};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

        [self.blueToothManager writeData:sendData];
    }
    if(button.tag == 2){
        NSLog(@"分针顺时针调");
        self.minutehands2 ++;
        self.minutehands2 = [XHbluetoolUtils intTransitionhex16:self.minutehands2];
        Byte send[] = {0x58,0x57,0x03,0x08,self.minutehands2,0x00,0x00,self.minutehands2};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

        [self.blueToothManager writeData:sendData];
    }
}



- (void)longPressGesture:(id)sender
{
    //监听长按的状态
    //    说明：长按手势的常用状态如下
    //   1 开始：UIGestureRecognizerStateBegan
    //   2 改变：UIGestureRecognizerStateChanged
    //   3 结束：UIGestureRecognizerStateEnded
    //   4 取消：UIGestureRecognizerStateCancelled
    //   5 失败：UIGestureRecognizerStateFailed
//    01：时针向左（不停 一直转）（校准模式使用）
//    02：时针向右（不停 一直转）（校准模式使用）
//    03：分针向左（不停 一直转）（校准模式使用）
//    04：分针向右（不停 一直转）（校准模式使用）
    UILongPressGestureRecognizer *lonPress = sender;
    NSLog(@"小时逆调不停 一直转");
    if (lonPress.state == UIGestureRecognizerStateBegan)
    {
             //your code
        NSLog(@"开始");
        Byte send[] = {0x58,0x57,0x03,0x01,0x00,0x00,0x00,0x00};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
             
        [self.blueToothManager writeData:sendData];
             
    }
    if(lonPress.state == UIGestureRecognizerStateEnded){
        
        Byte send[] = {0x58,0x57,0x03,0x00,0x00,0x00,0x00,0x00};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

        [self.blueToothManager writeData:sendData];
        
    }
}
- (void)longPressGesturetow:(id)sender
{
    //监听长按的状态
//    01：时针向左（不停 一直转）（校准模式使用）
//    02：时针向右（不停 一直转）（校准模式使用）
//    03：分针向左（不停 一直转）（校准模式使用）
//    04：分针向右（不停 一直转）（校准模式使用）
    UILongPressGestureRecognizer *lonPress = sender;
    NSLog(@"分针逆调不停 一直转");
    if (lonPress.state == UIGestureRecognizerStateBegan)
    {
             //your code
        NSLog(@"开始");
        Byte send[] = {0x58,0x57,0x03,0x03,0x00,0x00,0x00,0x00};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
             
        [self.blueToothManager writeData:sendData];
             
    }
    if(lonPress.state == UIGestureRecognizerStateEnded){
        
        Byte send[] = {0x58,0x57,0x03,0x00,0x00,0x00,0x00,0x00};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

        [self.blueToothManager writeData:sendData];
        
    }
}
- (void)longPressGesturejia:(id)sender{
    //    01：时针向左（不停 一直转）（校准模式使用）
    //    02：时针向右（不停 一直转）（校准模式使用）
    //    03：分针向左（不停 一直转）（校准模式使用）
    //    04：分针向右（不停 一直转）（校准模式使用）
        UILongPressGestureRecognizer *lonPress = sender;
        NSLog(@"小时顺调不停 一直转");
        if (lonPress.state == UIGestureRecognizerStateBegan)
        {
                 //your code
            NSLog(@"开始");
            Byte send[] = {0x58,0x57,0x03,0x02,0x00,0x00,0x00,0x00};
            NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
                 
            [self.blueToothManager writeData:sendData];
                 
        }
        if(lonPress.state == UIGestureRecognizerStateEnded){
            
            Byte send[] = {0x58,0x57,0x03,0x00,0x00,0x00,0x00,0x00};
            NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

            [self.blueToothManager writeData:sendData];
            
        }
}
- (void)longPressGesturetowjia:(id)sender{
    //    01：时针向左（不停 一直转）（校准模式使用）
    //    02：时针向右（不停 一直转）（校准模式使用）
    //    03：分针向左（不停 一直转）（校准模式使用）
    //    04：分针向右（不停 一直转）（校准模式使用）
        UILongPressGestureRecognizer *lonPress = sender;
        NSLog(@"分针顺调不停 一直转");
        if (lonPress.state == UIGestureRecognizerStateBegan)
        {
                 //your code
            NSLog(@"开始");
            Byte send[] = {0x58,0x57,0x03,0x04,0x00,0x00,0x00,0x00};
            NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
                 
            [self.blueToothManager writeData:sendData];
                 
        }
        if(lonPress.state == UIGestureRecognizerStateEnded){
            
            Byte send[] = {0x58,0x57,0x03,0x00,0x00,0x00,0x00,0x00};
            NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

            [self.blueToothManager writeData:sendData];
            
        }
}
-(void)back{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc{
    NSLog(@"离开了");
    if(!self.confirm ){
        NSLog(@"没有点击确定 自动存储00:00");
        Byte send[] = {0x58,0x57,0x03,0xff,0x00,0x00,0x00,0x01};
        NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];

        [self.blueToothManager writeData:sendData];
        NSString *time = @"00:00";
        //    存入蓝牙时间
        [[NSUserDefaults standardUserDefaults] setObject:time forKey:@"Bluetoothtime"];
        //这里建议同步存储到磁盘中，但是不是必须的
        [[NSUserDefaults standardUserDefaults] synchronize];

        [[NSNotificationCenter defaultCenter]postNotificationName:@"bluetoothTime" object:nil userInfo:@{@"time":time}];
        
        
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
